package com.joker.demo.admin.util;

import com.joker.demo.admin.domain.po.EnvironmentMonitorPO;
import com.joker.demo.admin.domain.vo.EnvironmentMonitorVO;
import com.joker.demo.core.domain.dto.EnvironmentMonitorDTO;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * 环境监控数据转换工具类
 *
 * @author : feixiang.li
 * @since : 2025-01-27 10:30
 */
public class EnvironmentMonitorConvertUtils {

    private EnvironmentMonitorConvertUtils() {

    }

    /**
     * 将DTO转换为PO
     *
     * @param dto 环境监控DTO
     * @return 环境监控PO
     */
    public static EnvironmentMonitorPO convertToPO(EnvironmentMonitorDTO dto) {
        if (dto == null) {
            return null;
        }

        EnvironmentMonitorPO po = new EnvironmentMonitorPO();

        // 设置基本信息
        po.setRegisterKey(dto.getRegisterKey());
        po.setAppName(dto.getAppName());
        po.setMonitorTime(LocalDateTime.ofInstant(
                new Date(dto.getTimestamp()).toInstant(),
                ZoneId.systemDefault()));

        // 设置系统信息
        if (dto.getSystemInfo() != null) {
            EnvironmentMonitorDTO.SystemInfo systemInfo = dto.getSystemInfo();
            po.setOsName(systemInfo.getOsName());
            po.setOsVersion(systemInfo.getOsVersion());
            po.setOsArch(systemInfo.getOsArch());
            po.setCpuCores(systemInfo.getCpuCores());
            po.setCpuUsagePercent(systemInfo.getCpuUsagePercent() != null ?
                    BigDecimal.valueOf(systemInfo.getCpuUsagePercent()) : null);
            po.setTotalMemoryMB(systemInfo.getTotalMemoryMB());
            po.setUsedMemoryMB(systemInfo.getUsedMemoryMB());
            po.setFreeMemoryMB(systemInfo.getFreeMemoryMB());
            po.setSystemMemoryUsagePercent(systemInfo.getSystemMemoryUsagePercent() != null ?
                    BigDecimal.valueOf(systemInfo.getSystemMemoryUsagePercent()) : null);
            po.setSystemLoadAverage(systemInfo.getSystemLoadAverage() != null ?
                    BigDecimal.valueOf(systemInfo.getSystemLoadAverage()) : null);
            po.setUptimeSeconds(systemInfo.getUptimeSeconds());
            
            // 设置系统内存相关的新字段
            po.setSystemMemoryTotalMB(systemInfo.getTotalMemoryMB());
            po.setSystemMemoryUsedMB(systemInfo.getUsedMemoryMB());
            po.setSystemMemoryFreeMB(systemInfo.getFreeMemoryMB());
        }

        // 设置JVM信息
        if (dto.getJvmInfo() != null) {
            EnvironmentMonitorDTO.JvmInfo jvmInfo = dto.getJvmInfo();
            po.setJvmVersion(jvmInfo.getJvmVersion());
            po.setJvmVendor(jvmInfo.getJvmVendor());
            po.setJvmName(jvmInfo.getJvmName());
            po.setJvmStartTime(jvmInfo.getStartTime());
            po.setJvmUptime(jvmInfo.getUptime());
            po.setMaxHeapMemoryMB(jvmInfo.getMaxHeapMemoryMB());
            po.setUsedHeapMemoryMB(jvmInfo.getUsedHeapMemoryMB());
            po.setFreeHeapMemoryMB(jvmInfo.getFreeHeapMemoryMB());
            po.setHeapMemoryUsagePercent(jvmInfo.getHeapMemoryUsagePercent() != null ?
                    BigDecimal.valueOf(jvmInfo.getHeapMemoryUsagePercent()) : null);
            po.setMaxNonHeapMemoryMB(jvmInfo.getMaxNonHeapMemoryMB());
            po.setUsedNonHeapMemoryMB(jvmInfo.getUsedNonHeapMemoryMB());
            po.setNonHeapMemoryUsagePercent(jvmInfo.getNonHeapMemoryUsagePercent() != null ?
                    BigDecimal.valueOf(jvmInfo.getNonHeapMemoryUsagePercent()) : null);
            po.setThreadCount(jvmInfo.getThreadCount());
            po.setDaemonThreadCount(jvmInfo.getDaemonThreadCount());
            po.setPeakThreadCount(jvmInfo.getPeakThreadCount());
            po.setClassLoaderCount(jvmInfo.getClassLoaderCount());
            po.setLoadedClassCount(jvmInfo.getLoadedClassCount());
            po.setTotalLoadedClassCount(jvmInfo.getTotalLoadedClassCount());
            po.setUnloadedClassCount(jvmInfo.getUnloadedClassCount());
            
            // 设置堆内存相关的新字段
            po.setHeapMemoryTotalMB(jvmInfo.getMaxHeapMemoryMB());
            po.setHeapMemoryUsedMB(jvmInfo.getUsedHeapMemoryMB());
            po.setHeapMemoryFreeMB(jvmInfo.getFreeHeapMemoryMB());
            
            // 设置非堆内存相关的新字段
            po.setNonHeapMemoryTotalMB(jvmInfo.getMaxNonHeapMemoryMB());
            po.setNonHeapMemoryUsedMB(jvmInfo.getUsedNonHeapMemoryMB());
            po.setNonHeapMemoryFreeMB(jvmInfo.getMaxNonHeapMemoryMB() != null && jvmInfo.getUsedNonHeapMemoryMB() != null ?
                    jvmInfo.getMaxNonHeapMemoryMB() - jvmInfo.getUsedNonHeapMemoryMB() : null);
        }

        // 设置磁盘信息 - 计算汇总数据而不是存储JSON
        if (dto.getDiskInfos() != null && !dto.getDiskInfos().isEmpty()) {
            // 计算所有磁盘的汇总信息
            long totalSpaceMB = 0;
            long usedSpaceMB = 0;
            long freeSpaceMB = 0;
            double totalUsagePercent = 0.0;
            int diskCount = 0;
            
            for (EnvironmentMonitorDTO.DiskInfo diskInfo : dto.getDiskInfos()) {
                if (diskInfo.getTotalSpaceMB() != null) {
                    totalSpaceMB += diskInfo.getTotalSpaceMB();
                    usedSpaceMB += (diskInfo.getUsedSpaceMB() != null ? diskInfo.getUsedSpaceMB() : 0);
                    freeSpaceMB += (diskInfo.getFreeSpaceMB() != null ? diskInfo.getFreeSpaceMB() : 0);
                    if (diskInfo.getDiskUsagePercent() != null) {
                        totalUsagePercent += diskInfo.getDiskUsagePercent();
                        diskCount++;
                    }
                }
            }
            
            // 设置磁盘汇总信息
            po.setDiskTotalGB(totalSpaceMB > 0 ? BigDecimal.valueOf(totalSpaceMB / 1024.0) : null);
            po.setDiskUsedGB(usedSpaceMB > 0 ? BigDecimal.valueOf(usedSpaceMB / 1024.0) : null);
            po.setDiskFreeGB(freeSpaceMB > 0 ? BigDecimal.valueOf(freeSpaceMB / 1024.0) : null);
            po.setDiskUsagePercent(diskCount > 0 ? BigDecimal.valueOf(totalUsagePercent / diskCount) : null);
        }

        // 设置网络信息 - 计算汇总数据而不是存储JSON
        if (dto.getNetworkInfo() != null) {
            EnvironmentMonitorDTO.NetworkInfo networkInfo = dto.getNetworkInfo();
            // 这里可以根据需要计算网络相关的汇总数据
            // 由于DTO中没有对应的字段，暂时设置为null
            po.setNetworkReceiveBytesPerSec(null);
            po.setNetworkSendBytesPerSec(null);
        }

        // 设置进程信息
        if (dto.getProcessInfo() != null) {
            EnvironmentMonitorDTO.ProcessInfo processInfo = dto.getProcessInfo();
            po.setProcessId(processInfo.getPid());
            po.setProcessName(processInfo.getProcessName());
            po.setProcessCpuUsagePercent(processInfo.getProcessCpuUsagePercent() != null ?
                    BigDecimal.valueOf(processInfo.getProcessCpuUsagePercent()) : null);
            po.setProcessMemoryUsageMB(processInfo.getProcessMemoryUsageMB());
            po.setProcessThreadCount(processInfo.getProcessThreadCount());
            po.setOpenFileCount(processInfo.getOpenFileCount());
            po.setProcessPriority(processInfo.getPriority());
        }

        // 设置死锁线程相关字段 - 由于DTO中没有对应字段，暂时设置为null
        po.setDeadlockedThreadCount(null);
        po.setDeadlockedMonitorThreadCount(null);

        return po;
    }

    /**
     * 将PO转换为DTO
     *
     * @param po 环境监控PO
     * @return 环境监控DTO
     */
    public static EnvironmentMonitorDTO convertToDTO(EnvironmentMonitorPO po) {
        if (po == null) {
            return null;
        }

        EnvironmentMonitorDTO dto = new EnvironmentMonitorDTO();

        // 设置基本信息
        dto.setRegisterKey(po.getRegisterKey());
        dto.setAppName(po.getAppName());
        dto.setTimestamp(po.getMonitorTime().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());

        // 设置系统信息
        EnvironmentMonitorDTO.SystemInfo systemInfo = new EnvironmentMonitorDTO.SystemInfo();
        systemInfo.setOsName(po.getOsName());
        systemInfo.setOsVersion(po.getOsVersion());
        systemInfo.setOsArch(po.getOsArch());
        systemInfo.setCpuCores(po.getCpuCores());
        systemInfo.setCpuUsagePercent(po.getCpuUsagePercent() != null ?
                po.getCpuUsagePercent().doubleValue() : null);
        systemInfo.setTotalMemoryMB(po.getTotalMemoryMB());
        systemInfo.setUsedMemoryMB(po.getUsedMemoryMB());
        systemInfo.setFreeMemoryMB(po.getFreeMemoryMB());
        systemInfo.setSystemMemoryUsagePercent(po.getSystemMemoryUsagePercent() != null ?
                po.getSystemMemoryUsagePercent().doubleValue() : null);
        systemInfo.setSystemLoadAverage(po.getSystemLoadAverage() != null ?
                po.getSystemLoadAverage().doubleValue() : null);
        systemInfo.setUptimeSeconds(po.getUptimeSeconds());
        dto.setSystemInfo(systemInfo);

        // 设置JVM信息
        EnvironmentMonitorDTO.JvmInfo jvmInfo = new EnvironmentMonitorDTO.JvmInfo();
        jvmInfo.setJvmVersion(po.getJvmVersion());
        jvmInfo.setJvmVendor(po.getJvmVendor());
        jvmInfo.setJvmName(po.getJvmName());
        jvmInfo.setStartTime(po.getJvmStartTime());
        jvmInfo.setUptime(po.getJvmUptime());
        jvmInfo.setMaxHeapMemoryMB(po.getMaxHeapMemoryMB());
        jvmInfo.setUsedHeapMemoryMB(po.getUsedHeapMemoryMB());
        jvmInfo.setFreeHeapMemoryMB(po.getFreeHeapMemoryMB());
        jvmInfo.setHeapMemoryUsagePercent(po.getHeapMemoryUsagePercent() != null ?
                po.getHeapMemoryUsagePercent().doubleValue() : null);
        jvmInfo.setMaxNonHeapMemoryMB(po.getMaxNonHeapMemoryMB());
        jvmInfo.setUsedNonHeapMemoryMB(po.getUsedNonHeapMemoryMB());
        jvmInfo.setNonHeapMemoryUsagePercent(po.getNonHeapMemoryUsagePercent() != null ?
                po.getNonHeapMemoryUsagePercent().doubleValue() : null);
        jvmInfo.setThreadCount(po.getThreadCount());
        jvmInfo.setDaemonThreadCount(po.getDaemonThreadCount());
        jvmInfo.setPeakThreadCount(po.getPeakThreadCount());
        jvmInfo.setClassLoaderCount(po.getClassLoaderCount());
        jvmInfo.setLoadedClassCount(po.getLoadedClassCount());
        jvmInfo.setTotalLoadedClassCount(po.getTotalLoadedClassCount());
        jvmInfo.setUnloadedClassCount(po.getUnloadedClassCount());

        // 不再解析GC信息JSON，因为不再存储JSON
        jvmInfo.setGcInfos(null);
        dto.setJvmInfo(jvmInfo);

        // 不再解析磁盘信息JSON，因为不再存储JSON
        dto.setDiskInfos(null);

        // 不再解析网络信息JSON，因为不再存储JSON
        dto.setNetworkInfo(null);

        // 设置进程信息
        EnvironmentMonitorDTO.ProcessInfo processInfo = new EnvironmentMonitorDTO.ProcessInfo();
        processInfo.setPid(po.getProcessId());
        processInfo.setProcessName(po.getProcessName());
        processInfo.setProcessCpuUsagePercent(po.getProcessCpuUsagePercent() != null ?
                po.getProcessCpuUsagePercent().doubleValue() : null);
        processInfo.setProcessMemoryUsageMB(po.getProcessMemoryUsageMB());
        processInfo.setProcessThreadCount(po.getProcessThreadCount());
        processInfo.setOpenFileCount(po.getOpenFileCount());
        processInfo.setPriority(po.getProcessPriority());
        dto.setProcessInfo(processInfo);

        return dto;
    }

    /**
     * 将PO转换为VO
     *
     * @param po 环境监控PO
     * @return 环境监控VO
     */
    public static EnvironmentMonitorVO convertToVO(EnvironmentMonitorPO po) {
        if (po == null) {
            return null;
        }

        EnvironmentMonitorVO vo = new EnvironmentMonitorVO();

        // 设置基本信息
        vo.setId(po.getId());
        vo.setRegisterKey(po.getRegisterKey());
        vo.setAppName(po.getAppName());
        vo.setMonitorTime(po.getMonitorTime());
        vo.setCreateTime(po.getCreateTime());
        vo.setUpdateTime(po.getUpdateTime());

        // 设置系统信息
        vo.setOsName(po.getOsName());
        vo.setOsVersion(po.getOsVersion());
        vo.setOsArch(po.getOsArch());
        vo.setCpuCores(po.getCpuCores());
        vo.setCpuUsagePercent(po.getCpuUsagePercent());
        vo.setTotalMemoryMB(po.getTotalMemoryMB());
        vo.setUsedMemoryMB(po.getUsedMemoryMB());
        vo.setFreeMemoryMB(po.getFreeMemoryMB());
        vo.setSystemMemoryUsagePercent(po.getSystemMemoryUsagePercent());
        vo.setSystemLoadAverage(po.getSystemLoadAverage());
        vo.setUptimeSeconds(po.getUptimeSeconds());

        // 设置JVM信息
        vo.setJvmVersion(po.getJvmVersion());
        vo.setJvmVendor(po.getJvmVendor());
        vo.setJvmName(po.getJvmName());
        vo.setJvmStartTime(po.getJvmStartTime());
        vo.setJvmUptime(po.getJvmUptime());
        vo.setMaxHeapMemoryMB(po.getMaxHeapMemoryMB());
        vo.setUsedHeapMemoryMB(po.getUsedHeapMemoryMB());
        vo.setFreeHeapMemoryMB(po.getFreeHeapMemoryMB());
        vo.setHeapMemoryUsagePercent(po.getHeapMemoryUsagePercent());
        vo.setMaxNonHeapMemoryMB(po.getMaxNonHeapMemoryMB());
        vo.setUsedNonHeapMemoryMB(po.getUsedNonHeapMemoryMB());
        vo.setNonHeapMemoryUsagePercent(po.getNonHeapMemoryUsagePercent());
        vo.setThreadCount(po.getThreadCount());
        vo.setDaemonThreadCount(po.getDaemonThreadCount());
        vo.setPeakThreadCount(po.getPeakThreadCount());
        vo.setClassLoaderCount(po.getClassLoaderCount());
        vo.setLoadedClassCount(po.getLoadedClassCount());
        vo.setTotalLoadedClassCount(po.getTotalLoadedClassCount());
        vo.setUnloadedClassCount(po.getUnloadedClassCount());

        // 不再设置JSON字段，因为不再存储JSON信息
        vo.setDiskInfosJson(null);
        vo.setNetworkInfoJson(null);
        vo.setGcInfosJson(null);

        // 设置进程信息
        vo.setProcessId(po.getProcessId());
        vo.setProcessName(po.getProcessName());
        vo.setProcessCpuUsagePercent(po.getProcessCpuUsagePercent());
        vo.setProcessMemoryUsageMB(po.getProcessMemoryUsageMB());
        vo.setProcessThreadCount(po.getProcessThreadCount());
        vo.setOpenFileCount(po.getOpenFileCount());
        vo.setProcessPriority(po.getProcessPriority());

        return vo;
    }
}
